package com.passer.web.travel.controller;

import java.util.List;

import com.passer.web.travel.domain.TImages;
import com.passer.web.travel.domain.TUser;
import com.passer.web.travel.service.ITImagesService;
import com.passer.web.travel.service.ITUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.passer.common.annotation.Log;
import com.passer.common.enums.BusinessType;
import com.passer.web.travel.domain.TYouji;
import com.passer.web.travel.service.ITYoujiService;
import com.passer.common.core.controller.BaseController;
import com.passer.common.core.domain.AjaxResult;
import com.passer.common.utils.poi.ExcelUtil;
import com.passer.common.core.page.TableDataInfo;

/**
 * 游记攻略Controller
 *
 * @author passer
 * @date 2021-11-23
 */
@Controller
@RequestMapping("/travel/youji")
public class TYoujiController extends BaseController
{
    private String prefix = "travel/youji";

    @Autowired
    private ITYoujiService tYoujiService;

    @Autowired
    private ITUserService tUserService;

    @Autowired
    private ITImagesService tImagesService;

    @RequiresPermissions("travel:youji:view")
    @GetMapping()
    public String youji()
    {
        return prefix + "/youji";
    }

    /**
     * 查询游记攻略列表
     */
    @RequiresPermissions("travel:youji:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TYouji tYouji)
    {
        startPage();
        List<TYouji> list = tYoujiService.selectTYoujiList(tYouji);
        return getDataTable(list);
    }

    /**
     * 导出游记攻略列表
     */
    @RequiresPermissions("travel:youji:export")
    @Log(title = "游记攻略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TYouji tYouji)
    {
        List<TYouji> list = tYoujiService.selectTYoujiList(tYouji);
        ExcelUtil<TYouji> util = new ExcelUtil<TYouji>(TYouji.class);
        return util.exportExcel(list, "游记攻略数据");
    }

    /**
     * 新增游记攻略
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        // TODO: 2021/11/30 所有客户信息(用于选择作者)
        List<TUser> users = tUserService.selectTUserList(null);
        mmap.put("users", users);
        return prefix + "/add";
    }

    /**
     * 新增保存游记攻略
     */
    @RequiresPermissions("travel:youji:add")
    @Log(title = "游记攻略", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TYouji tYouji)
    {
        int i = tYoujiService.insertTYouji(tYouji);
        //插入封面图片到图片库中
        tImagesService.saveCover(tYouji);
        return toAjax(i);
    }

    /**
     * 修改游记攻略
     */
    @GetMapping("/edit/{yid}")
    public String edit(@PathVariable("yid") Long yid, ModelMap mmap)
    {
        TYouji tYouji = tYoujiService.selectTYoujiById(yid);
        tYouji.setCoverURL(tImagesService.selectCoverByBusiness(tYouji).getSrc());
        mmap.put("tYouji", tYouji);

        // TODO: 2021/11/30 所有客户信息(用于选择作者)
        List<TUser> users = tUserService.selectTUserList(null);
        mmap.put("users", users);

        return prefix + "/edit";
    }

    /**
     * 修改保存游记攻略
     */
    @RequiresPermissions("travel:youji:edit")
    @Log(title = "游记攻略", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TYouji tYouji)
    {
        int i = tYoujiService.updateTYouji(tYouji);
        //插入封面图片到图片库中
        tImagesService.saveCover(tYouji);
        return toAjax(i);
    }


    /**
     * 删除游记攻略
     */
    @RequiresPermissions("travel:youji:remove")
    @Log(title = "游记攻略", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tYoujiService.deleteTYoujiByIds(ids));
    }
}
